package com.yanqu.road.server.cmd.union.cross.trade;

import com.yanqu.road.pb.union.UnionTradeProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.union.cross.CrossUnionTradeModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

@Cmd(code = Protocol.S_UNION_TRADE_ROUTE_LIST_DATA, desc = "路线信息")
public class GetTradeRouteListCmd extends AbstractCommand {

    private int routeId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionTradeProto.RouteListReqMsg reqMsg = UnionTradeProto.RouteListReqMsg.parseFrom(packet.getBytes());
        routeId = reqMsg.getRouteId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        String unionUid = player.getUserInfo().getUnionUid();
        if(StringUtils.isNullOrEmpty(unionUid)){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }

        if (player.isRequestCmdTooFast(Protocol.S_UNION_TRADE_ROUTE_LIST_DATA, 500)) {
            ret = GameErrorCode.E_REQUEST_TOO_FAST;
            return;
        }

        UnionTradeProto.RouteListCrossReqMsg.Builder reqMsg = UnionTradeProto.RouteListCrossReqMsg.newBuilder();
        reqMsg.setRouteId(routeId);
        List<Integer> hadSendList = player.getModule(CrossUnionTradeModule.class).getSendPatronsIdList();
        reqMsg.addAllSendList(hadSendList);
        player.sendPacket(Protocol.C_UNION_TRADE_ROUTE_LIST_DATA, reqMsg);

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            UnionTradeProto.RouteListRespMsg.Builder  routeListRespMsg = UnionTradeProto.RouteListRespMsg.newBuilder();
            routeListRespMsg.setRet(ret);
            player.sendPacket(Protocol.U_UNION_TRADE_ROUTE_LIST_DATA, routeListRespMsg);
        }
    }
}
